package com.murphy.algorithm.likou;

/**
 * 二叉树最小深度
 * @author dongsufeng
 * @version 4.0
 * @date 2023/2/24 12:38
 */
public class MinDepth {

    public int min(TreeNode treeNode){

        if (treeNode == null){
            return 0;
        }
        int tl = min(treeNode.l);
        int tr = min(treeNode.r);
        if (tl > tr){
            return tr+1;
        }else {
            return tl +1;
        }
    }

    public int max(TreeNode treeNode){

        if (treeNode == null){
            return 0;
        }
        int tl = max(treeNode.l);
        int tr = max(treeNode.r);
        if (tl < tr){
            return tr+1;
        }else {
            return tl +1;
        }
    }


    public static class TreeNode {
        private int val;
        private TreeNode l;
        private TreeNode r;

        public TreeNode() {
        }

        public TreeNode(int val) {
            this.val = val;
        }
    }

    public static void main(String[] args) {
        LowestCommonAncestor lowestCommonAncestor = new LowestCommonAncestor();
        TreeNode treeNode = new TreeNode(5);
        TreeNode treeNode3 = new TreeNode(3);
        treeNode.l = treeNode3;
        TreeNode treeNode2 = new TreeNode(2);
        treeNode.l.l = treeNode2;
//        TreeNode treeNode4 = new TreeNode(4);
//        treeNode.l.r = treeNode4;

        TreeNode treeNode7 = new TreeNode(7);
        treeNode.r = treeNode7;
//        TreeNode treeNode6 = new TreeNode(6);
//        treeNode.r.l = treeNode6;
//        TreeNode treeNode8 = new TreeNode(8);
//        treeNode.r.r = treeNode8;

        MinDepth minDepth = new MinDepth();
//        System.out.println(minDepth.min(treeNode));
        System.out.println(minDepth.max(treeNode));
    }
}
